package com.fancon.android.gardenjokesx.widget;

import net.londatiga.android.ActionItem;
import net.londatiga.android.QuickAction;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.fancon.android.gardenjokesx.R;
import com.fancon.android.gardenjokesx.activity.ComicMakerActivity;
import com.fancon.android.gardenjokesx.activity.GardenJokesActivity;
import com.fancon.android.gardenjokesx.activity.P06Activity;
import com.fancon.android.gardenjokesx.activity.ProfileSettingActivity;
/**
 * Top Control for application
 * 
 * @author admin
 * 
 */
public class GardenControlView extends RelativeLayout {
	// action id
	private static final int ID_SHARE = 1;
	private static final int ID_FEEDBACK = 2;
	private static final int ID_SETTING = 4;
	private static final int ID_ABOUT = 5;

	private View refresh, refreshing, moreBtn, newJokeBtn, drawRageBtn;
	private Context mContext;
	public GardenControlView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		// TODO Auto-generated constructor stub
		LayoutInflater layoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layoutInflater.inflate(R.layout.garden_top_layout, this);

		refresh = findViewById(R.id.refresh);
		refresh.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				((GardenJokesActivity)mContext).getCurrentIRefresh().doRefresh(arg0);
			}
		});
		refreshing = findViewById(R.id.refreshing);
		moreBtn = findViewById(R.id.more_btn);
		newJokeBtn = findViewById(R.id.new_joke);
		drawRageBtn = findViewById(R.id.draw_rage);
		drawRageBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(mContext,
						ComicMakerActivity.class);
				mContext.startActivity(intent);
			}
		});
		newJokeBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(mContext,
						P06Activity.class);
				mContext.startActivity(intent);
			}
		});
		buildMenu();
	}

	private void buildMenu() {
		ActionItem shareItem = new ActionItem(ID_SHARE,
				mContext.getString(R.string.share), mContext.getResources()
						.getDrawable(R.drawable.share));
		ActionItem feedbackItem = new ActionItem(ID_FEEDBACK,
				mContext.getString(R.string.feed_back), mContext.getResources()
						.getDrawable(R.drawable.comment));
		//ActionItem moreAppsItem = new ActionItem(ID_INFO,
		//		mContext.getString(R.string.more_apps), mContext.getResources()
		//				.getDrawable(R.drawable.more_apps));
		ActionItem aboutItem = new ActionItem(ID_ABOUT,
				mContext.getString(R.string.about_apps), mContext
						.getResources().getDrawable(R.drawable.icon));
		ActionItem settingItem = new ActionItem(ID_SETTING,
				mContext.getString(R.string.setting), mContext.getResources()
						.getDrawable(R.drawable.settings));

		// use setSticky(true) to disable QuickAction dialog being dismissed
		// after an item is clicked
		// shareItem.setSticky(true);

		// create QuickAction. Use QuickAction.VERTICAL or
		// QuickAction.HORIZONTAL param to define layout
		// orientation
		final QuickAction quickAction = new QuickAction(mContext,
				QuickAction.VERTICAL);

		// add action items into QuickAction
		quickAction.addActionItem(shareItem);
		quickAction.addActionItem(feedbackItem);
		//quickAction.addActionItem(moreAppsItem);
		quickAction.addActionItem(settingItem);
		quickAction.addActionItem(aboutItem);

		// Set listener for action item clicked
		quickAction
				.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
					@Override
					public void onItemClick(QuickAction source, int pos,
							int actionId) {
						//ActionItem actionItem = quickAction.getActionItem(pos);

						// here we can filter which action item was clicked with
						// pos or actionId parameter
						if (actionId == ID_SHARE) {
							doShare();
						} else if (actionId == ID_FEEDBACK) {
							doFeedBack();
						} else if (actionId == ID_ABOUT) {
							// goto app website
							Intent browserIntent = new Intent(Intent.ACTION_VIEW,
				                    Uri.parse(linkContainHttp(mContext.getString(R.string.common_web_app_about))));
							mContext.startActivity(browserIntent);
						} else if (actionId == ID_SETTING) {
							Intent intent = new Intent(mContext,
									ProfileSettingActivity.class);
							mContext.startActivity(intent);
						} else {

						}
					}
				});
		moreBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				quickAction.show(v);
			}
		});
	}

	public void showRefresh() {
		refresh.setVisibility(View.VISIBLE);
		refreshing.setVisibility(View.GONE);
	}

	public void showRefreshing() {
		refresh.setVisibility(View.GONE);
		refreshing.setVisibility(View.VISIBLE);
	}
	
	/**
	 * Share information of this app to social netwoek
	 */
	public void doShare() {
		Intent share = new Intent(Intent.ACTION_SEND);
		share.setType("text/plain");
		share.putExtra(Intent.EXTRA_TITLE, mContext.getResources()
				.getString(R.string.share_jorke));
		share.putExtra(Intent.EXTRA_TEXT, mContext.getResources()
				.getString(R.string.share_jorke_invite));
		mContext.startActivity(Intent.createChooser(share, mContext.getResources()
				.getString(R.string.share_jorke)));
	}
	
	public void doFeedBack() {
		
		//Intent i = new Intent(Intent.ACTION_SEND);
		Intent mailler = new Intent(Intent.ACTION_SEND);
//		share.setType("text/plain");
		mailler.setType("message/rfc822");
		mailler.putExtra(Intent.EXTRA_EMAIL, new String[] { mContext.getResources().getString(R.string.common_email_support) });
		mailler.putExtra(Intent.EXTRA_SUBJECT, mContext.getResources()
				.getString(R.string.feedback_title));
		mailler.putExtra(Intent.EXTRA_TEXT, "");
		mContext.startActivity(Intent.createChooser(mailler, mContext.getResources()
				.getString(R.string.feedback)));
	}
	/**
	  * Generate standard link to open web Page
	  * 
	 * @param url
	 * @return standard url
	 */
		public String linkContainHttp(String url) {
		        String link = "";
		        if (url.startsWith("http")) {
		            return url;
		        } else {
		            link = "http://" + url;
		        }
		        return link;
		    }
}
